<?php

namespace org;

use org\Curl;
use think\Db;

class Yundan {

    public $url = "https://izop.zt-express.com/oms/api?method=addBbcImportOrder&timestamp={currentTimestamp}&appCode={appCode}";
    public $dev_url = "https://izop-test.zt-express.com/oms/api?method=addBbcImportOrder&timestamp={currentTimestamp}&appCode={appCode}";
    
    
    
    public $appCode = "10676";
    public $secretkey = "7r*cQSA#";

    function send($orderNo) {
        $orderinfo = Db::name('order')->where(['order_id' => $orderNo])->find();
        $order_items = Db::name('order_items')->where(['order_id' => $orderNo])->select();
        $intlOrderItemList = [];
        $weight = 0;
        $quantity = 0;
        foreach ($order_items as $list) {
            $spes_desc = Db::name('goods')->where(['id' => $list['goods_id']])->value('params');
            $sku = Db::name('goods')->where(['id' => $list['goods_id']])->value('lib_code');
            $itemId = Db::name('goods')->where(['id' => $list['goods_id']])->value('lib_code');
            $spes_arr = unserialize($spes_desc);
            $intlOrderItemList[] = [
                "itemId" => $itemId,
                "itemName" => $list['name'],
                "itemUnitPrice" => $list['price'],
                "itemQuantity" => $list['nums'],
                "currencyType" => "CNY",
                "itemUnit" => "KG"
            ];

            $weight = $weight + $spes_arr['单品重量'];
            $quantity++;
        }

        $data = [
            "logisticsno" => "",
            "orderno" => $orderNo,
            // 
            "shipper" => "洋浦亿通发国际贸易有限公司",
            "shipperprov" => "海南省",
            "shippercity" => "儋州市",
            "shipperdistrict" => "洋浦经济开发区",
            "shipperaddress" => "洋浦保税港区仓库",
            "shippermobile" => "18889423533",
            "shippertelephone"=>"",
            "shippercountry" => "中国",
            //    
            "consignee" => $orderinfo['ship_name'],
            "consigneeprov" => getprovince($orderinfo['ship_area_id']),
            "consigneecity" => getcity($orderinfo['ship_area_id']),
            "consigneedistrict" => getarea($orderinfo['ship_area_id']),
            "consigneeaddress" => $orderinfo['ship_address'],
            "consigneemobile" => $orderinfo['ship_mobile'],
            "consigneetelephone" => $orderinfo['ship_mobile'],
            "consigneecountry" => "中国",
            //   
            "idtype" => 1,
            "customerid" => $orderinfo['buyer_idcard'],
            //  
            "weight" => $weight,
            "ietype" => "I",
            "stockflag" => 2,
            //  
            "cumstomscode" => "OTHERCUSTOMS",
            "platformSource" => "10676", ////////////////////
            "warehouseCode" => "au002",
            //
            "netweight" => $weight,
            "shipType" => "2",
            //
            "billEntity" => [
                "quantity" => $quantity,
                "ecpcode" => "4606660094",
                "ecpname" => "洋浦亿通发国际贸易有限公司",
                "ecpcodeG" => "4606660094",
                "ecpnameG" => "洋浦亿通发国际贸易有限公司",
                "wraptype" => "2",
                "companyCode" => "6408"
            ],
            "intlOrderItemList" => $intlOrderItemList
        ];
        
        $timeline = time() * 1000;
        $str_one = json_encode($data);
        $arr = array(
            "sign" => md5($timeline . $this->secretkey . $str_one),
            "data" => $str_one
        );
        $des = new Mcrypt();
        $des->secretkey = $this->secretkey;

        $str_u = $des->encrypt(json_encode($arr));
        $url = str_replace("{currentTimestamp}", $timeline, str_replace("{appCode}", $this->appCode, $this->dev_url));
        $r = Curl::http_post_json($url, $str_u);
        //print_r($r);
        $res=json_decode($r[1],true);
        $res_data = $des->decrypt($res['data'],true);
        
        
        //echo $res_data;exit;
        return json_decode($res_data, 1);
    }

}
